<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class sharecontrol extends control {
	
	public function __construct() {
		$aC = $this->_chechlogin(); //检查用户登陆
		all::$aU = model::member()->get( $aC['id'] );
	}
	
	public function ondown() {
		$fid = func::input( 'id', 'i' );  //得到所下载文件的ID
		if ( empty( $fid ) ) die();
		$re = model::share()->get( $fid );

		$file = DIR_DAT . "share/{$re['filename']}.dat";
		if( file_exists( $file ) ) {
			$filename = "{$re['oldname']}.{$re['ext']}";
			header( 'Content-type: '. $re['mime'] );
			if ( 'htm' != $re['icon'] ) header( 'Content-Disposition: attachment; filename='. $filename  );
			readfile( $file );
		}
		exit;
	}
	
	public function onup() { //添加共享文件
		$brief = func::input ( 'brief' );
		$page_curr = func::input( 'page', 'i' );  //当前页

		$error  = '';
		$error .= ( '' == $_FILES['file']['name'] ) ? '请选择文件' : '';
		$error .= ( $brief == '' ) ? '请写文件说明' : '';
		$error .= ( 0 < $_FILES['file']['error'] ) ? '文件上传过程出错' : '';
		$error .= ( $_FILES['file']['size'] > all::$cfg['class']['file_max_size'] * 1024 ) ? '上传文件超过限制大小' : '';
		$share_used = model::share()->getused();
		$error .= ( $share_used + $_FILES['file']['size'] > all::$cfg['class']['share_volume'] * 1024 * 1024) ? '文件总量超过相册容量限制' : '';
		
		if ( $error != '' ) { func::showerr( $error ); } //提示出错信息，上传失败
		if ( model::share()->up( all::$aU['id'], $brief ) ) {
			model::score()->member_score( all::$aU['id'], all::$cfg['suser']['upshare'] ); //用户积分增加
		}
		func::redirect( "./index.php?m=share&page=$page_curr" ); //添加成功，转向
	}
	
	public function ondel() {//删除共享文件
		$page_curr = func::input( 'page', 'i' );  //当前页
		$fid = func::input( 'id', 'i' );
		
		$re = model::share()->get( $fid );
		if ( ( all::$aU['power'] > 1 ) || ( all::$aU['id'] == $re['owner'] ) ) { model::share()->del( $fid, $re['filename'] ); }
		
		func::redirect( "./index.php?m=share&page=$page_curr" ); //添加成功，转向
	}
	
	public function onindex() {
		all::$aOut['sms_num'] = model::sms()->checknew( all::$aU['id'] ); //小纸条
		all::$aOut['mod_name'] = all::$cfg['navname']['share']; //模块名称
		all::$aOut['top10'] = model::member()->gettop10();//输出班级会员积分 TOP 10

		$page_curr = func::input( 'page', 'i' );  //当前页

		$page_size = all::$cfg['class']['file_per_page'];       //指定分页每一页多少条数据
		$limit = show_page( $page_size, 'limit' );  //设定当前页中的SQL的限制

		all::$aOut['shs'] = model::share()->getpart( $limit );//print_r(all::$aOut['shs']);
		$page_tip = show_page( $page_size );   //得到分页结果
		all::$aOut['page_tip'] = $page_tip;
		all::$aOut['page'] = $page_curr;

		$sh_num = count( all::$aOut['shs'] );          //共享文件数 总空间
		$sh_used = 0;
		for ( $i = 0; $i < $sh_num; $i++ ) {
			$sh_used += all::$aOut['shs'][$i]['ksize'];
		}
		all::$aOut['sh_total'] = all::$cfg['class']['share_volume'];
		all::$aOut['sh_num'] = $sh_num;
		all::$aOut['sh_used'] = $sh_used;
		all::$aOut['sh_percent'] = round( $sh_used * 100 / ( all::$cfg['class']['share_volume'] * 1024 ) ); //已用空间百分比
		
		func::display( all::$aOut, 'share.htm' );
	}
	
}
